package org.msfj.toilets.socket;

import java.io.*;
import java.net.Socket;
import java.util.Random;

/**
 * @Description:
 * @Author: like
 * @Date: 2018-03-09 10:21
 * @Version: 1.0
 * @Created in idea by autoCode
 */

public class TcpClient {

    private static byte[] temperature = {
            (byte) 0xfe, (byte) 0x5c, (byte) 0x4b, (byte) 0x89,
            (byte) 0x27, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x65,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x14, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x03, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x05, (byte) 0x32, (byte) 0x31, (byte) 0x2e, (byte) 0x31, (byte) 0x30,
            (byte) 0x04, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x05, (byte) 0x32, (byte) 0x35, (byte) 0x2e, (byte) 0x33, (byte) 0x37,
            (byte) 0xff, (byte) 0xff};
    private static byte[] infrared = {
            (byte) 0xfe, (byte) 0x5c, (byte) 0x4b, (byte) 0x89,
            (byte) 0x20, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x65,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x0d, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0x06, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) 0xff, (byte) 0xff};

    public static void main(String[] args) throws IOException {
        try {
            while (true) {
                // 1、创建客户端Socket，指定服务器地址和端口
                // Socket socket=new Socket("127.0.0.1",5200);
                Socket socket = new Socket("127.0.0.1", 8081);
                System.out.println("客户端启动成功");
                while (true) {
                    OutputStream outputStream = socket.getOutputStream();
                    Random random = new Random();
                    int i = random.nextInt(10);
                    if (i >= 5) {
                        outputStream.write(temperature);
                    } else {
                        outputStream.write(infrared);
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("can not listen to:" + e);// 出错，打印出错信息
        }
    }

}
